/**
 * 视频数据获取服务
 * 实现步骤三：循环模拟点击商品列表搜索到的商品 -> 跳转到对应的商品详情页 -> 根据接口获取视频数据详情
 */

const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const axios = require('axios');
const ProductVideo = require('../models/ProductVideo');
const { Op } = require('sequelize');

// 使用stealth插件避免被检测
puppeteer.use(StealthPlugin());

class VideoDataService {
    constructor() {
        this.browser = null;
        this.requestCount = 0;
        this.lastRequestTime = 0;
        this.minDelay = 2000; // 最小间隔2秒
        this.maxDelay = 5000; // 最大间隔5秒

        // Shoplus视频数据API配置
        this.shoplusConfig = {
            baseURL: 'https://www.shoplus.net/api/v1',
            authToken: this.loadAuthTokenFromEnv()
        };
    }

    /**
     * 从环境变量加载authToken配置
     */
    loadAuthTokenFromEnv() {
        // 检查是否有环境变量配置
        if (process.env.SHOPLUS_AUTH_NAME &&
            process.env.SHOPLUS_AUTH_EXPIRES &&
            process.env.SHOPLUS_AUTH_ACCESS_TOKEN &&
            process.env.SHOPLUS_AUTH_USER_ID) {

            console.log('📋 VideoDataService: 从环境变量加载authToken配置');
            return {
                name: process.env.SHOPLUS_AUTH_NAME,
                expires: parseInt(process.env.SHOPLUS_AUTH_EXPIRES),
                accessToken: process.env.SHOPLUS_AUTH_ACCESS_TOKEN,
                userId: parseInt(process.env.SHOPLUS_AUTH_USER_ID),
                phone: process.env.SHOPLUS_AUTH_PHONE || process.env.SHOPLUS_AUTH_NAME,
                locale: process.env.SHOPLUS_AUTH_LOCALE || 'en'
            };
        } else {
            console.log('📋 VideoDataService: 使用默认硬编码authToken配置');
            // 回退到硬编码配置
            return {
                "name": "13143359181",
                "expires": 1757322261597,
                "accessToken": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJ7XCJhY2NvdW50TnVtYmVyXCI6XCIxMzE0MzM1OTE4MVwiLFwiYWNjb3VudFR5cGVcIjowLFwiYXBwbHlTdGF0dXNcIjoyLFwiYnVzaW5lc3NUeXBlXCI6MixcImZyb21TeXN0ZW1cIjoxMyxcImlkXCI6NTQwMTUyLFwiaXNFeHBpcmVcIjpmYWxzZSxcImp3dElkXCI6XCI5NDRjMmM2NTdhNTQ0ODJkOWRkMzRkM2Q3YmI2ZDc4OFwiLFwibG9naW5OYW1lXCI6XCIxMzE0MzM1OTE4MVwiLFwibWVyY2hhbnRJZFwiOjUzMTU0OCxcInByb2ZpbGVcIjpcImdjcF91c193ZXN0MVwiLFwic3lzdGVtSWRcIjo4LFwidG9rZW5FeHBpcmVEYXRlXCI6MTc1NzMyMjI2MTQwNyxcIndlY2hhdFN1YlwiOmZhbHNlfSIsImlzcyI6Inp6LXNhYXMtcGVyaW1zc2lvbiIsImlhdCI6MTc1NjcxNzQ2MSwianRpIjoiOTQ0YzJjNjU3YTU0NDgyZDlkZDM0ZDNkN2JiNmQ3ODgifQ.AGhNKdNQfUi7FoxHBgiL33157ls6y8zUfmk-G585rX8",
                "userId": 540152,
                "phone": "13143359181",
                "locale": "en"
            };
        }
    }

    /**
     * 控制访问频次
     */
    async controlRequestFrequency() {
        const now = Date.now();
        const timeSinceLastRequest = now - this.lastRequestTime;
        const randomDelay = Math.random() * (this.maxDelay - this.minDelay) + this.minDelay;
        const waitTime = Math.max(0, randomDelay - timeSinceLastRequest);

        if (waitTime > 0) {
            console.log(`⏱️  访问频次控制: 等待 ${Math.round(waitTime / 1000)} 秒...`);
            await new Promise(resolve => setTimeout(resolve, waitTime));
        }

        this.lastRequestTime = Date.now();
        this.requestCount++;
    }

    /**
     * 初始化浏览器
     */
    async initBrowser() {
        if (!this.browser) {
            const launchOptions = {
                headless: 'new',
                timeout: 60000,
                args: [
                    '--no-sandbox',
                    '--disable-setuid-sandbox',
                    '--disable-dev-shm-usage',
                    '--disable-accelerated-2d-canvas',
                    '--no-first-run',
                    '--no-zygote',
                    '--disable-gpu',
                    '--window-size=1920,1080'
                ],
                defaultViewport: {
                    width: 1920,
                    height: 1080
                }
            };

            // macOS特定配置
            if (process.platform === 'darwin') {
                const possiblePaths = [
                    '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
                    '/Applications/Chromium.app/Contents/MacOS/Chromium'
                ];

                for (const path of possiblePaths) {
                    try {
                        const fs = require('fs');
                        if (fs.existsSync(path)) {
                            launchOptions.executablePath = path;
                            break;
                        }
                    } catch (e) {
                        // 继续尝试下一个路径
                    }
                }
            }

            this.browser = await puppeteer.launch(launchOptions);
        }
        return this.browser;
    }

    /**
     * 创建新页面
     */
    async createPage() {
        const browser = await this.initBrowser();
        const page = await browser.newPage();

        // 设置用户代理
        await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');

        // 设置请求拦截，只加载必要资源
        await page.setRequestInterception(true);
        page.on('request', (req) => {
            const resourceType = req.resourceType();
            if (resourceType === 'stylesheet' || resourceType === 'font' || resourceType === 'image') {
                req.abort();
            } else {
                req.continue();
            }
        });

        return page;
    }

    /**
     * 根据商品ID获取Shoplus上的视频数据（通过模拟点击）
     */
    async getVideoDataFromShoplus(productId, productName, customAuthToken = null) {
        try {
            console.log(`🔍 模拟点击Shoplus商品详情页获取视频数据: ${productName}`);
            if (customAuthToken) {
                console.log('🔑 使用自定义authToken获取视频数据');
            }

            // 先尝试通过模拟点击获取数据
            const browserResult = await this.getVideoDataByBrowserSimulation(productId, customAuthToken);

            if (browserResult.success) {
                console.log(`✅ 通过模拟点击成功获取 ${browserResult.videos.length} 个视频数据`);
                return browserResult;
            }

            console.log(`⚠️ 模拟点击失败，尝试API方式: ${browserResult.error}`);

            // 如果模拟点击失败，回退到API方式
            return await this.getVideoDataByAPI(productId, productName, customAuthToken);

        } catch (error) {
            console.error(`❌ 获取视频数据异常:`, error.message);
            return {
                success: false,
                error: error.message,
                videos: []
            };
        }
    }

    /**
     * 通过模拟点击Shoplus商品详情页获取视频数据
     */
    async getVideoDataByBrowserSimulation(productId, customAuthToken = null) {
        let page = null;

        try {
            console.log(`🖱️ 开始模拟点击Shoplus商品详情页...`);

            page = await this.createPage();

            // 构建Shoplus商品详情页链接
            const shoplusUrl = `https://www.shoplus.net/goods/detail/${productId}?type=videos&countryCode=MY`;
            console.log(`🌐 访问链接: ${shoplusUrl}`);

            // 访问页面
            await page.goto(shoplusUrl, {
                waitUntil: 'networkidle2',
                timeout: 30000
            });

            // 等待页面加载完成
            await this.wait(3000);

            // 检查是否需要登录
            const needLogin = await this.checkIfLoginRequired(page);
            if (needLogin) {
                console.log(`🔑 检测到需要登录，尝试处理...`);
                const loginSuccess = await this.handleLogin(page);
                if (!loginSuccess) {
                    return {
                        success: false,
                        error: 'LOGIN_REQUIRED',
                        videos: []
                    };
                }
            }

            // 尝试点击切换到Relevant Videos标签页
            console.log(`🎯 尝试切换到Relevant Videos标签页...`);
            const switchSuccess = await this.switchToRelevantVideosTab(page);

            if (switchSuccess) {
                console.log(`✅ 成功切换到Relevant Videos标签页`);
                // 使用Relevant Videos API获取数据
                const relevantVideosData = await this.getRelevantVideosFromAPI(productId, customAuthToken);
                if (relevantVideosData.success) {
                    return relevantVideosData;
                }
            }

            // 如果切换失败或API调用失败，回退到原有的页面抓取方式
            console.log(`⚠️ Relevant Videos获取失败，使用页面抓取方式`);

            // 等待视频数据加载
            console.log(`⏳ 等待视频数据加载...`);
            await page.waitForSelector('.video-list, [class*="video"], [class*="Video"]', {
                timeout: 15000
            }).catch(() => {
                console.log(`⚠️ 未找到视频列表选择器，继续尝试其他方式`);
            });

            // 滚动页面加载更多视频
            await this.scrollToLoadMoreVideos(page);

            // 提取视频数据
            const videoData = await this.extractVideoDataFromPage(page);

            console.log(`📊 从页面提取到 ${videoData.length} 个视频数据`);

            return {
                success: true,
                videos: videoData,
                totalCount: videoData.length
            };

        } catch (error) {
            console.error(`❌ 模拟点击失败:`, error.message);
            return {
                success: false,
                error: error.message,
                videos: []
            };
        } finally {
            if (page) {
                await page.close();
            }
        }
    }

    /**
     * 通过API方式获取视频数据（备用方案）
     */
    async getVideoDataByAPI(productId, productName, customAuthToken = null) {
        try {
            console.log(`📡 通过API获取视频数据: ${productName}`);

            // 选择使用自定义token或默认token
            const authToken = customAuthToken || this.shoplusConfig.authToken;

            // 检查token是否过期
            if (Date.now() > authToken.expires) {
                throw new Error('Shoplus授权token已过期');
            }

            // 对授权token进行编码
            const encodedAuthToken = encodeURIComponent(JSON.stringify(authToken));

            const url = `${this.shoplusConfig.baseURL}/product/video-list`;
            const params = {
                product_id: productId,
                cursor: 0,
                count: 50,
                sort: 'play_count',
                sort_type: 'DESC'
            };

            const headers = {
                'authorized-token': encodedAuthToken,
                'Accept': 'application/json, text/plain, */*',
                'Accept-Language': 'en-US,en;q=0.9',
                'Cache-Control': 'no-cache',
                'Pragma': 'no-cache',
                'Referer': 'https://www.shoplus.net/',
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
            };

            const response = await axios.get(url, {
                params: params,
                headers: headers,
                timeout: 30000
            });

            if (response.status === 200 && response.data && response.data.code === 0) {
                const videos = response.data.data || [];
                console.log(`✅ API方式成功获取 ${videos.length} 个视频数据`);
                return {
                    success: true,
                    videos: videos,
                    totalCount: videos.length
                };
            } else {
                return {
                    success: false,
                    error: response.data?.msg || 'API_ERROR',
                    videos: []
                };
            }

        } catch (error) {
            console.error(`❌ API方式获取失败:`, error.message);
            return {
                success: false,
                error: error.message,
                videos: []
            };
        }
    }

    /**
     * 解析并格式化视频数据
     */
    parseVideoData(videoData, productId) {
        try {
            // 处理sys_country嵌套对象
            const sysCountry = videoData.sys_country || {};

            return {
                id: videoData.id || videoData.video_id,
                origin_cover: videoData.origin_cover || videoData.cover,
                product_id: productId,
                desc: videoData.desc || videoData.description,
                video_duration: videoData.video_duration || videoData.duration,
                video_link: this.generateVideoLink(videoData.author_unique_id, videoData.id),
                total_estimated_sales_amount: this.parseAmount(videoData.total_estimated_sales_amount),
                total_estimated_sales_volume: videoData.total_estimated_sales_volume || 0,
                play_count: videoData.play_count || 0,
                digg_count: videoData.digg_count || 0,
                comment_count: videoData.comment_count || 0,
                share_count: videoData.share_count || 0,
                author_last_time: videoData.author_last_time,
                author_last_time_stamp: this.parseTimestamp(videoData.author_last_time_stamp),
                author_id: videoData.author_id,
                author_unique_id: videoData.author_unique_id,
                author_nickname: videoData.author_nickname,
                // 处理嵌套的sys_country对象
                sys_country_country: sysCountry.country || videoData.sys_country_country,
                sys_country_country_cn: sysCountry.country_cn || videoData.sys_country_country_cn,
                sys_country_country_code: sysCountry.country_code || videoData.sys_country_country_code,
                author_follower_count: videoData.author_follower_count || 0,
                author_homepage: this.generateAuthorHomepage(videoData.author_unique_id)
            };
        } catch (error) {
            console.error('解析视频数据失败:', error);
            return null;
        }
    }

    /**
     * 生成视频链接
     */
    generateVideoLink(authorUniqueId, videoId) {
        if (!authorUniqueId || !videoId) {
            return null;
        }
        return `https://www.tiktok.com/@${authorUniqueId}/video/${videoId}`;
    }

    /**
     * 生成KOL主页链接
     */
    generateAuthorHomepage(authorUniqueId) {
        if (!authorUniqueId) {
            return null;
        }
        return `https://www.tiktok.com/@${authorUniqueId}`;
    }

    /**
     * 解析金额字符串
     */
    parseAmount(amountStr) {
        if (!amountStr) return 0;

        const str = amountStr.toString().toUpperCase();
        const numMatch = str.match(/[\d.]+/);
        if (!numMatch) return 0;

        const num = parseFloat(numMatch[0]);
        if (isNaN(num)) return 0;

        if (str.includes('K')) {
            return num * 1000;
        } else if (str.includes('M')) {
            return num * 1000000;
        } else if (str.includes('B')) {
            return num * 1000000000;
        }

        return num;
    }

    /**
     * 解析时间戳
     */
    parseTimestamp(timestamp) {
        if (!timestamp) return null;

        try {
            // 如果是数字时间戳
            if (typeof timestamp === 'number') {
                return new Date(timestamp * 1000);
            }

            // 如果是字符串，尝试解析
            if (typeof timestamp === 'string') {
                const num = parseInt(timestamp);
                if (!isNaN(num)) {
                    return new Date(num * 1000);
                }
                return new Date(timestamp);
            }

            return new Date(timestamp);
        } catch (error) {
            console.log('时间戳解析失败:', timestamp);
            return null;
        }
    }

    /**
     * 保存视频数据到数据库
     */
    async saveVideoData(videoData) {
        try {
            const formattedData = this.parseVideoData(videoData, videoData.product_id);
            if (!formattedData) {
                return { success: false, error: '数据格式化失败' };
            }

            // 使用upsert避免重复插入
            const [video, created] = await ProductVideo.upsert(formattedData, {
                where: { id: formattedData.id }
            });

            return {
                success: true,
                created: created,
                video: video
            };

        } catch (error) {
            console.error('保存视频数据失败:', error);
            return {
                success: false,
                error: error.message
            };
        }
    }

    /**
     * 批量保存视频数据
     */
    async batchSaveVideoData(videosData, productId) {
        const results = {
            success: 0,
            failed: 0,
            total: videosData.length,
            errors: []
        };

        for (const videoData of videosData) {
            try {
                videoData.product_id = productId;
                const result = await this.saveVideoData(videoData);

                if (result.success) {
                    results.success++;
                    console.log(`✅ 保存视频成功: ${videoData.id || 'unknown'}`);
                } else {
                    results.failed++;
                    results.errors.push(result.error);
                    console.log(`❌ 保存视频失败: ${result.error}`);
                }
            } catch (error) {
                results.failed++;
                results.errors.push(error.message);
                console.log(`❌ 保存视频异常: ${error.message}`);
            }
        }

        return results;
    }

    /**
     * 处理单个商品的视频数据获取
     */
    async processProductVideos(productId, productName, customAuthToken = null) {
        try {
            console.log(`\n🎯 开始处理商品视频数据: ${productName} (ID: ${productId})`);
            if (customAuthToken) {
                console.log('🔑 使用自定义authToken');
            } else {
                console.log('🔑 使用默认authToken');
            }

            // 控制访问频次
            await this.controlRequestFrequency();

            // 从Shoplus获取视频数据
            const videoResult = await this.getVideoDataFromShoplus(productId, productName, customAuthToken);

            if (!videoResult.success) {
                return {
                    success: false,
                    productId: productId,
                    productName: productName,
                    error: videoResult.error,
                    videoCount: 0
                };
            }

            // 批量保存视频数据
            const saveResult = await this.batchSaveVideoData(videoResult.videos, productId);

            console.log(`📊 商品 ${productName} 视频处理完成:`);
            console.log(`   - 总数: ${saveResult.total}`);
            console.log(`   - 成功: ${saveResult.success}`);
            console.log(`   - 失败: ${saveResult.failed}`);

            return {
                success: true,
                productId: productId,
                productName: productName,
                videoCount: saveResult.success,
                totalVideos: saveResult.total,
                failedVideos: saveResult.failed,
                errors: saveResult.errors
            };

        } catch (error) {
            console.error(`❌ 处理商品视频失败: ${productName}`, error);
            return {
                success: false,
                productId: productId,
                productName: productName,
                error: error.message,
                videoCount: 0
            };
        }
    }

    /**
     * 批量处理多个商品的视频数据
     */
    async batchProcessProductVideos(products) {
        console.log(`\n🚀 开始批量处理 ${products.length} 个商品的视频数据\n`);

        const results = {
            success: 0,
            failed: 0,
            total: products.length,
            totalVideos: 0,
            details: []
        };

        for (let i = 0; i < products.length; i++) {
            const product = products[i];
            console.log(`\n📋 进度: ${i + 1}/${products.length}`);

            try {
                const result = await this.processProductVideos(product.id, product.productName);
                results.details.push(result);

                if (result.success) {
                    results.success++;
                    results.totalVideos += result.videoCount;
                } else {
                    results.failed++;
                }

                // 每5个商品后额外休息
                if ((i + 1) % 5 === 0 && i < products.length - 1) {
                    console.log(`\n⏸️  处理了 ${i + 1} 个商品，休息 3 秒...`);
                    await new Promise(resolve => setTimeout(resolve, 3000));
                }

            } catch (error) {
                console.error(`❌ 处理商品异常: ${product.productName}`, error);
                results.failed++;
                results.details.push({
                    success: false,
                    productId: product.id,
                    productName: product.productName,
                    error: error.message,
                    videoCount: 0
                });
            }
        }

        return results;
    }

    /**
     * 尝试切换到Relevant Videos标签页
     */
    async switchToRelevantVideosTab(page) {
        try {
            console.log(`🔍 查找Relevant Videos标签...`);

            // 尝试多种可能的标签选择器
            const tabSelectors = [
                'text="Relevant Videos"',
                '[data-testid*="relevant"]',
                '[class*="relevant"]',
                'text="相关视频"',
                '.ant-tabs-tab:contains("Relevant")',
                '.tab-item:contains("Relevant")',
                'a[href*="relevant"]',
                'button:contains("Relevant")',
                '.nav-item:contains("Relevant")'
            ];

            for (const selector of tabSelectors) {
                try {
                    // 等待元素出现
                    await page.waitForSelector(selector, { timeout: 2000 });

                    console.log(`✅ 找到Relevant Videos标签: ${selector}`);

                    // 模拟点击
                    await page.click(selector);
                    console.log(`🖱️ 已点击Relevant Videos标签`);

                    // 等待内容加载
                    await this.wait(2000);

                    return true;
                } catch (e) {
                    // 继续尝试下一个选择器
                    continue;
                }
            }

            console.log(`⚠️ 未找到Relevant Videos标签`);
            return false;

        } catch (error) {
            console.error(`❌ 切换到Relevant Videos标签失败:`, error.message);
            return false;
        }
    }

    /**
     * 通过Relevant Videos API获取相关视频数据
     */
    async getRelevantVideosFromAPI(productId, customAuthToken = null) {
        try {
            console.log(`📡 调用Relevant Videos API获取相关视频...`);

            // 选择使用自定义token或默认token
            const authToken = customAuthToken || this.shoplusConfig.authToken;

            // 检查token是否过期
            if (Date.now() > authToken.expires) {
                throw new Error('Shoplus授权token已过期');
            }

            // 对授权token进行编码
            const encodedAuthToken = encodeURIComponent(JSON.stringify(authToken));

            // 使用您提供的Relevant Videos API接口
            const url = `${this.shoplusConfig.baseURL}/product/related-videos`;
            const params = {
                is_commerce: true,
                sort: 18,
                cursor: 0,
                sort_type: 'DESC',
                size: 20,
                product_id: productId
            };

            const headers = {
                'authorized-token': encodedAuthToken,
                'Accept': 'application/json, text/plain, */*',
                'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
                'Cache-Control': 'no-cache',
                'Pragma': 'no-cache',
                'Referer': `https://www.shoplus.net/goods/detail/${productId}?type=videos&countryCode=MY`,
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
            };

            console.log(`🔗 请求URL: ${url}`);
            console.log(`📝 请求参数:`, params);

            const response = await axios.get(url, {
                params: params,
                headers: headers,
                timeout: 30000
            });

            console.log(`📨 API响应状态: ${response.status}`);

            if (response.status === 200 && response.data && response.data.code === 0) {
                const videos = response.data.data || [];
                console.log(`✅ Relevant Videos API成功获取 ${videos.length} 个相关视频`);

                return {
                    success: true,
                    videos: videos,
                    totalCount: videos.length,
                    apiType: 'relevant-videos'
                };
            } else {
                console.log(`❌ Relevant Videos API返回错误:`, response.data);
                return {
                    success: false,
                    error: response.data?.msg || 'RELEVANT_API_ERROR',
                    videos: []
                };
            }

        } catch (error) {
            console.error(`❌ Relevant Videos API调用失败:`, error.message);
            return {
                success: false,
                error: error.message,
                videos: []
            };
        }
    }

    /**
     * 检查是否需要登录
     */
    async checkIfLoginRequired(page) {
        try {
            // 检查常见的登录提示
            const loginSelectors = [
                'button[class*="login"]',
                'a[href*="login"]',
                '.login-required',
                '[class*="Login"]',
                'text="登录"',
                'text="Login"'
            ];

            for (const selector of loginSelectors) {
                const element = await page.$(selector);
                if (element) {
                    console.log(`🔍 检测到登录元素: ${selector}`);
                    return true;
                }
            }

            // 检查URL是否跳转到登录页
            const currentUrl = page.url();
            if (currentUrl.includes('/login') || currentUrl.includes('/auth')) {
                console.log(`🔍 检测到登录页面跳转: ${currentUrl}`);
                return true;
            }

            return false;
        } catch (error) {
            console.log(`⚠️ 检查登录状态失败: ${error.message}`);
            return false;
        }
    }

    /**
     * 处理登录（如果需要）
     */
    async handleLogin(page) {
        try {
            console.log(`🔑 尝试处理登录...`);

            // 这里可以添加自动登录逻辑
            // 目前返回false，需要手动处理
            console.log(`⚠️ 需要手动登录，无法自动处理`);
            return false;

        } catch (error) {
            console.error(`❌ 处理登录失败: ${error.message}`);
            return false;
        }
    }

    /**
     * 滚动页面加载更多视频
     */
    async scrollToLoadMoreVideos(page) {
        try {
            console.log(`📜 滚动页面加载更多视频...`);

            // 获取初始视频数量
            let previousCount = 0;
            let currentCount = 0;
            let scrollAttempts = 0;
            const maxScrolls = 5;

            while (scrollAttempts < maxScrolls) {
                // 计算当前视频数量
                currentCount = await page.$$eval('[class*="video"], [class*="Video"]', elements => elements.length).catch(() => 0);

                console.log(`📊 当前视频数量: ${currentCount}`);

                // 如果没有新视频加载，停止滚动
                if (currentCount === previousCount && scrollAttempts > 0) {
                    console.log(`✅ 没有更多视频，停止滚动`);
                    break;
                }

                previousCount = currentCount;

                // 滚动到页面底部
                await page.evaluate(() => {
                    window.scrollTo(0, document.body.scrollHeight);
                });

                // 等待新内容加载
                await this.wait(2000);

                scrollAttempts++;
            }

            console.log(`📜 滚动完成，共进行 ${scrollAttempts} 次滚动`);

        } catch (error) {
            console.log(`⚠️ 滚动加载失败: ${error.message}`);
        }
    }

    /**
     * 从页面提取视频数据
     */
    async extractVideoDataFromPage(page) {
        try {
            console.log(`🔍 开始从页面提取视频数据...`);

            const videoData = await page.evaluate(() => {
                const videos = [];

                // 尝试多种选择器来找到视频元素
                const videoSelectors = [
                    '[class*="video-item"]',
                    '[class*="video-card"]',
                    '[class*="VideoItem"]',
                    '[class*="VideoCard"]',
                    '.video-list .video',
                    '[data-testid*="video"]',
                    'tr[class*="video"]', // 表格行形式
                    '.ant-table-tbody tr' // Ant Design 表格
                ];

                let videoElements = [];

                for (const selector of videoSelectors) {
                    videoElements = document.querySelectorAll(selector);
                    if (videoElements.length > 0) {
                        console.log(`找到 ${videoElements.length} 个视频元素，使用选择器: ${selector}`);
                        break;
                    }
                }

                videoElements.forEach((element, index) => {
                    try {
                        const video = {
                            id: null,
                            desc: null,
                            play_count: 0,
                            digg_count: 0,
                            comment_count: 0,
                            share_count: 0,
                            author_unique_id: null,
                            author_nickname: null,
                            author_follower_count: 0,
                            video_duration: 0,
                            total_estimated_sales_amount: 0,
                            total_estimated_sales_volume: 0,
                            origin_cover: null,
                            sys_country_country: 'MY'
                        };

                        // 提取视频ID
                        const idElement = element.querySelector('[data-video-id], [data-id]');
                        if (idElement) {
                            video.id = idElement.getAttribute('data-video-id') || idElement.getAttribute('data-id');
                        }

                        // 如果没有找到ID，生成一个临时ID
                        if (!video.id) {
                            video.id = `shoplus_video_${Date.now()}_${index}`;
                        }

                        // 提取描述（尝试多种可能的选择器）
                        const descSelectors = ['.desc', '.description', '[class*="desc"]', 'td:nth-child(2)', '.ant-table-cell'];
                        for (const selector of descSelectors) {
                            const descElement = element.querySelector(selector);
                            if (descElement && descElement.textContent?.trim()) {
                                video.desc = descElement.textContent.trim();
                                break;
                            }
                        }

                        // 提取数值数据（播放量、点赞数等）
                        const textContent = element.textContent || '';
                        const numbers = textContent.match(/\d+[kKmM]?/g) || [];

                        // 尝试从文本中提取数字
                        if (numbers.length > 0) {
                            video.play_count = this.parseNumberFromText(numbers[0]) || 0;
                        }
                        if (numbers.length > 1) {
                            video.digg_count = this.parseNumberFromText(numbers[1]) || 0;
                        }

                        // 提取作者信息
                        const authorSelectors = ['[class*="author"]', '[class*="user"]', '[class*="creator"]'];
                        for (const selector of authorSelectors) {
                            const authorElement = element.querySelector(selector);
                            if (authorElement && authorElement.textContent?.trim()) {
                                video.author_nickname = authorElement.textContent.trim();
                                break;
                            }
                        }

                        // 提取封面图
                        const coverElement = element.querySelector('img');
                        if (coverElement) {
                            video.origin_cover = coverElement.src;
                        }

                        videos.push(video);

                    } catch (error) {
                        console.log(`提取第 ${index} 个视频数据失败: ${error.message}`);
                    }
                });

                return videos;
            });

            // 为每个视频生成链接
            videoData.forEach(video => {
                if (video.author_unique_id && video.id) {
                    video.video_link = this.generateVideoLink(video.author_unique_id, video.id);
                    video.author_homepage = this.generateAuthorHomepage(video.author_unique_id);
                }
            });

            console.log(`✅ 成功提取 ${videoData.length} 个视频数据`);
            return videoData;

        } catch (error) {
            console.error(`❌ 提取视频数据失败: ${error.message}`);
            return [];
        }
    }

    /**
     * 从文本中解析数字（处理K、M等单位）
     */
    parseNumberFromText(text) {
        if (!text) return 0;

        const str = text.toString().toUpperCase();
        const numMatch = str.match(/[\d.]+/);
        if (!numMatch) return 0;

        const num = parseFloat(numMatch[0]);
        if (isNaN(num)) return 0;

        if (str.includes('K')) {
            return Math.floor(num * 1000);
        } else if (str.includes('M')) {
            return Math.floor(num * 1000000);
        } else if (str.includes('B')) {
            return Math.floor(num * 1000000000);
        }

        return Math.floor(num);
    }

    /**
     * 等待指定时间
     */
    async wait(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    /**
     * 关闭浏览器
     */
    async closeBrowser() {
        if (this.browser) {
            await this.browser.close();
            this.browser = null;
            console.log('🔒 浏览器已关闭');
        }
    }

    /**
     * 获取统计信息
     */
    getStats() {
        return {
            requestCount: this.requestCount,
            lastRequestTime: this.lastRequestTime,
            uptime: Date.now() - (this.lastRequestTime - this.requestCount * 3000)
        };
    }
}

module.exports = new VideoDataService();